<?php
/**
 *
 * @author 凡墙<jihaoju@qq.com>
 * @date 2018/7/13 14:59
 * @description
 */

namespace apps\marriage\core\model;


use vm\com\BaseModel;

class User extends BaseModel
{
    // 头像审核状态
    const AVATAR_AUDIT_STATUS_AUDITING = 10;     // 审核中
    const AVATAR_AUDIT_STATUS_APPROVED = 20;     // 审核通过
    const AVATAR_AUDIT_STATUS_REJECT = 30;     // 未通过

    const VIRTUAL_MOBILE_PHONE_PREFIX = '000';

    public function increaseBalance($id, $amount)
    {
        $result = $this->where('id', '=', $id)->setInc('balance', $amount);
        return $this->getNumRows() > 0;
    }

    public function decreaseBalance($id, $amount)
    {
        $result = $this->where('id', '=', $id)->setDec('balance', $amount);
        return $this->getNumRows() > 0;
    }

    public function increaseWithdrawBalance($id, $amount)
    {
        $result1 = $this->where('id', '=', $id)->inc('balance', $amount)->inc('withdraw_balance', $amount)->update();
        return $this->getNumRows() > 0;
    }

    public function decreaseWithdrawBalance($id, $amount)
    {
        $result1 = $this->where('id', '=', $id)
            ->where('balance', ">=", $amount)
            ->where('withdraw_balance', ">=", $amount)
            ->dec('balance', $amount)
            ->dec('withdraw_balance', $amount)
            ->update();
        return $this->getNumRows() > 0;
    }

    public function applyWithdraw($id, $amount)
    {
        $result1 = $this->where('id', '=', $id)
            ->where('balance', ">=", $amount)
            ->where('withdraw_balance', ">=", $amount)
            ->where('withdrawing_balance', 0)
            ->dec('balance', $amount)
            ->dec('withdraw_balance', $amount)
            ->inc('withdrawing_balance', $amount)
            ->update();
        return $this->getNumRows() > 0;
    }

    public function rejectWithdraw($id, $amount)
    {
        $result1 = $this->where('id', '=', $id)
                        ->where('withdrawing_balance', ">=", $amount)
                        ->inc('balance', $amount)
                        ->inc('withdraw_balance', $amount)
                        ->dec('withdrawing_balance', $amount)
                        ->update();
        return $this->getNumRows() > 0;
    }

    public function finishWithdraw($id, $amount)
    {
        $result1 = $this->where('id', '=', $id)
                        ->where('withdrawing_balance', ">=", $amount)
                        ->dec('withdrawing_balance', $amount)
                        ->update();
        return $this->getNumRows() > 0;
    }

    /**
     * 按条件分页查询
     * @param array $params
     * @param $pageSize
     * @param array $paginateConfig
     * @return array|null
     * @throws \think\exception\DbException
     */
    public function pageListByParams(array $params, $pageSize, array $paginateConfig = [], array $sortOrder = [])
    {
        if(empty($params)) {
            return $this->pageList($pageSize, false, $paginateConfig);
        }
        $pk = $this->getPk();
        $query = $this;
        foreach ($params as $key => $val) {
            if($key == 'matchmaker_uid') {
                $query = $query->whereRaw("CONCAT(',', matchmaker_uids, ',') LIKE '%,$val,%'");
            } else {
                $query = $query->where($key, $val);
            }
        }
        if(empty($sortOrder)) {
            $sortOrder = [$pk => 'desc'];
        }
        $paginateConfig['list_rows'] = $pageSize;
        $data = $query->order($sortOrder)->paginate($paginateConfig, false)->toArray();
        return $data;
    }
}